package com.junlong0716.enjoylearning.ui

import android.support.v7.app.AppCompatActivity
import android.os.Bundle
import android.text.TextUtils
import android.view.View
import com.blankj.utilcode.util.ToastUtils
import com.junlong0716.enjoylearning.MyApp
import com.junlong0716.enjoylearning.R
import com.junlong0716.enjoylearning.constant.NetConstant
import com.junlong0716.enjoylearning.net.api.NetApi
import com.lzy.okgo.OkGo
import com.lzy.okgo.callback.StringCallback
import com.lzy.okgo.model.Response
import kotlinx.android.synthetic.main.activity_feedback.*
import kotlinx.android.synthetic.main.view_title_bar.*
import org.json.JSONObject

class FeedbackActivity : AppCompatActivity(), View.OnClickListener {

    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        setContentView(R.layout.activity_feedback)

        rl_back.setOnClickListener(this)
        tv_title.text = "用户反馈"
        bt_confirm.setOnClickListener(this)
    }


    override fun onClick(v: View?) {
        when (v!!.id) {
            R.id.rl_back -> {
                finish()
            }
            R.id.bt_confirm -> {
                val detText = det_feedback.text.toString().trim()
                if (TextUtils.isEmpty(detText)) {
                    ToastUtils.showShort("请输入反馈内容！")
                    return
                }

                val map = HashMap<String, Any>()
                map["userId"] = MyApp.instance().userAccount!!.id
                map["feedbackContent"] = detText
                val jsonObj = JSONObject(map)

                OkGo.post<String>(NetConstant.REQUEST_HEADER + NetApi.API_FEEDBACK)
                        .tag(this)
                        .upJson(jsonObj)
                        .execute(object : StringCallback() {
                            override fun onSuccess(response: Response<String>?) {
                                val jsonObj1 = JSONObject(response!!.body())
                                val code = jsonObj1.getString("code")
                                if (code == "200") {
                                    det_feedback.setText("")
                                    ToastUtils.showShort("反馈成功！")
                                }
                            }
                        })
            }
            else -> {
            }
        }
    }
}
